<script setup lang="ts">
import axios from "axios";
import { ElMessage } from "element-plus";
import { onMounted } from "vue";
import Top from "./components/home/TopBar.vue";
import { useLogging } from "./stores/logging";
import type { ResultContext } from "./types/ResultContext";
import type { User } from "./types/User";

const logging = useLogging();

onMounted(async () => {
  const userId = localStorage.getItem("userId");

  if (userId) {
    try {
      const response = await axios.get<ResultContext<User>>(
        `http://localhost:8080/api/auth/heartbeat/${userId}`,
      );
      logging.login(response.data.data);
    } catch (_error) {
      ElMessage.error("由于您长时间未登录，请重新登录。");
      localStorage.removeItem("userId");
    }
  }
});
</script>

<template>
  <Top></Top>
  <div class="main-content">
    <router-view></router-view>
  </div>
</template>

<style scoped>
.main-content {
  padding-top: 75px; /* 留出顶部导航高度 */
}
</style>
